package com.sqstudio.view
{
	import com.sqstudio.common.DataProxy;
	import com.sqstudio.common.LunarCalendar;
	import com.sqstudio.event.CalendarEvent;
	import com.sqstudio.model.Model;
	import com.sqstudio.ui.GridItemMC;
	import com.sqstudio.vo.ItemVo;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextFormat;
	
	/**
	 *日期表格Item 
	 * @author Nestor
	 * 
	 */	
	public class GridItem extends Sprite
	{
		private var _itemVo:ItemVo;
		
		
		private var _w:int = 55;
		private var _h:Number;
		
		private var _itemMc:GridItemMC;
		public function GridItem($vo:ItemVo)
		{
			this._itemVo = $vo;
			this._h = this._itemVo.isBig?44:37;
			this._itemMc = new GridItemMC();
			addChild(_itemMc);
			
			this._itemMc.overRect_mc.visible = this._itemMc.nongTxt.selectable = this._itemMc.gongTxt.selectable=false;
			
			this._itemMc.holiday_mc.visible = this._itemVo.isHoliday;
			this._itemMc.work_mc.visible = this._itemVo.isWork;
			this._itemMc.itemBg_mc.visible = this._itemVo.isCrtDay;
			this._itemMc.itemBg_mc.y--;
			this.mouseChildren = false;
			
			this._itemMc.itemBg_mc.width = this._itemMc.overRect_mc.width = this._w;
			this._itemMc.itemBg_mc.height = this._itemMc.overRect_mc.height = this._h;
			
			if(this._itemVo.isBig){
				this._itemMc.gongTxt.setTextFormat(new TextFormat(null,18,0x0000));
			}else{
				this._itemMc.gongTxt.setTextFormat(new TextFormat(null,15,0x0000));
			}
			this._itemMc.nongTxt.width=this._itemMc.nongTxt.textWidth+5;
			this._itemMc.nongTxt.height=this._itemMc.nongTxt.textHeight+2;
			
			this._itemMc.gongTxt.width=this._itemMc.gongTxt.textWidth+5;
			this._itemMc.gongTxt.height=this._itemMc.gongTxt.textHeight+2;
			
			this._itemMc.gongTxt.x = this._w - this._itemMc.gongTxt.width - 2;
			this._itemMc.gongTxt.y = 0;
			this._itemMc.nongTxt.x = 2;
			this._itemMc.nongTxt.y = this._h - 2 - this._itemMc.nongTxt.textHeight;
			
			this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
			drawBackGround();
			setTxtColor();
			setTxtName();
		}
		/**
		 *绘制背景 
		 * 
		 */		
		private function drawBackGround():void
		{
			this.graphics.lineStyle(0.5,0x666666,0.5);
			this.graphics.beginFill(0xffffff,0.1);
			this.graphics.drawRect(0,0,this._w,this._h);
			this.graphics.endFill();
		}
		
		/**
		 *加载到舞台 
		 * @param e
		 * 
		 */		
		private function addToStage(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE,removeFromStage);
			this.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
			this.addEventListener(MouseEvent.CLICK,itemCKHandler);
		}		
		/**
		 *鼠标点击响应 
		 * @param e
		 * 
		 */		
		private function itemCKHandler(e:MouseEvent):void
		{
			if(!DataProxy.equalsDate(this._itemVo.date,Model.CrtDate)){
				Model.CrtDate = DataProxy.getDateCopy(this._itemVo.date);
				CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.DATE_CHANGE));	
			}
		}
		/**
		 *鼠标滑过响应 
		 * @param e
		 * 
		 */		
		private function overHandler(e:MouseEvent):void
		{
			this._itemMc.overRect_mc.visible = true;
		}
		/**
		 *鼠标滑出响应 
		 * @param e
		 * 
		 */		
		private function outHandler(e:MouseEvent):void
		{
			this._itemMc.overRect_mc.visible = false;
		}
		/**
		 *从舞台上移除 
		 * @param e
		 * 
		 */		
		private function removeFromStage(e:Event):void
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE,removeFromStage);
			this.removeEventListener(MouseEvent.MOUSE_OVER,overHandler);
			this.removeEventListener(MouseEvent.MOUSE_OUT,outHandler);
			this.removeEventListener(MouseEvent.CLICK,itemCKHandler);
		}
		
		private function setTxtColor():void{
			var color:uint = 0x000000
			if(this._itemVo.isCrtDay){
				color = 0xffffff;
			}else if(!this._itemVo.isCrtMonth){
				color = 0x999999;
			}
			var tmf:TextFormat = new TextFormat();
			tmf.color = color;
			this._itemMc.nongTxt.defaultTextFormat = this._itemMc.gongTxt.defaultTextFormat = tmf;
			this._itemMc.nongTxt.setTextFormat(tmf);
			this._itemMc.gongTxt.setTextFormat(tmf);
			
		}

		private function setTxtName():void{
			var nongStr:String = this._itemVo.lc.getsFtv();
			nongStr =nongStr ==""?this._itemVo.lc.getwFtv():nongStr;
			nongStr =nongStr ==""?this._itemVo.lc.getlFtv():nongStr;
			nongStr =nongStr ==""?this._itemVo.lc.getCnSolarTerm():nongStr;
			nongStr =nongStr ==""?this._itemVo.lc.getCnDay():nongStr;
			this._itemMc.gongTxt.text = String(this._itemVo.lc.getGDate());
			this._itemMc.nongTxt.text = nongStr;
		
		}
		
		
	}
}